<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户登录名:" prop="username">
            <el-input v-model="formModel.username" disabled>
              <el-button slot="append" type="primary" icon="el-icon-search" :disabled="disabledUser" @click="queryUser"></el-button>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="业务类型:" prop="businessType">
            <hey-select v-model="formModel.businessType" :disabled="disabledType" @change="changeType" clcd="business_type_cd" />
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="showAuthorityList">
          <el-form-item label="权限列表:" prop="authorityList">
            <el-transfer
              style="text-align: left; display: inline-block"
              filterable
              v-model="formModel.authorityList"
              :titles="['未拥有权限列表', '已拥有权限列表']"
              :button-texts="['到左边', '到右边']"
              :data="authorityOptions">
            </el-transfer>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
    <el-dialog
      v-dialogDrag
      v-if="showUser"
      :close-on-click-modal="false"
      title="用户信息"
      :visible.sync="showUser"
      width="90%"
      append-to-body
    >
      <user-dialog @queryUserSuc="queryUserSuc"/>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'BusinessAuthorityMnt',
  components: {
    UserDialog: () => import('@/views/dialog/user-dialog')
  },
  props: ['pageType', 'editData'],
  data() {
    return {
      authorityOptions:[],
      showUser:false,
      disabledType:false,
      disabledUser:false,
      showAuthorityList:false,
      formModel: {
        userid: '',
        username: '',
        authorityList: [],
        businessType: '',
      },
      rules: {

      }
    }
  },
  computed: {

  },
  mounted() {
    this.init()
  },
  methods: {
    queryUser(){
      this.showUser = true
    },
    queryUserSuc(data){
      this.formModel.username = data.username
      this.formModel.userid = data.userid
    },
    changeType(value){
      if(value){
        this.formModel.authorityList = []
        this.authorityOptions = []
        //通过业务类型获取权限列表
        if(value=='01'){
          //电检信息
          const param = {pageNum:1,pageSize:9999}
          param.isDelete = '0'
          this.get('/tmBasModule/queryTmBasModule', param).then(res => {
            const arr = res.list
            if(arr&&arr.length>0){
              for (let i = 0; i < arr.length; i++) {
                this.authorityOptions.push({
                  key: arr[i].tmBasModuleId,
                  label: arr[i].moduleCode + '-' + arr[i].moduleDesc,
                  disabled: false
                });
              }
            }
            this.showAuthorityList = true
          })
        }
        //后续如果新增其他业务类型逻辑,则继续扩展
      }else{
        this.formModel.authorityList = []
        this.authorityOptions = []
        this.showAuthorityList = false
      }
    },
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType === 'edit') {
        this.changeType(editData.businessType)
        this.assignData(editData, formModel)
        this.disabledType = true
        this.disabledUser = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        username: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        businessType: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        authorityList: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { pageType, formModel } = this
          if (formModel.authorityList.length == 0) {
            this.$message.warning('请配置该用户的权限!')
            return false
          }
          const param = { ...formModel }
          const operateType = pageType === 'edit' ? '02' : '01'
          param.operateType = operateType
          const url = '/user/maintainUserBusinessAuthority'
          const tip = pageType === 'add' ? '新增成功！' : '修改成功！'
          this.post(url, param).then(res => {
            this.$info(tip, () => {
              this.$emit('mntSuc', res)
              this.closeDialog()
            })
          })
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
